/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.jpa;

import java.util.Iterator;
import java.util.Map;

import javax.persistence.Query;

/**
 * A utility for helping with create ejb3 queries.
 * 
 * @author boudyacho
 * @version $Id: QueryUtil.java,v 1.5 2007/06/29 18:16:16 chunyun Exp $
 * 
 */
public class QueryUtil {

	/**
	 * Add an AND parameter.
	 * 
	 * @param b
	 * @param parameters
	 * @param alias
	 * @param name
	 * @param operator
	 * @param value
	 */
	static public void formatAnd(StringBuffer b,
			Map<String, Object> parameters, String alias, String name,
			String operator, Object value) {
		if (value != null) {
			if (b.length() == 0) {
				b.append(" where ");
			} else {
				b.append(" and ");
			}
			b.append(alias);
			b.append(".");
			b.append(name);
			b.append(" ");
			b.append(operator);
			b.append(" :");
			b.append(name);
			if ("like".equals(operator)) {
				parameters.put(name, "%" + value + "%");
			} else {
				parameters.put(name, value);
			}
		}
	}

	static public void formatUnaryAnd(StringBuffer b,
			Map<String, Object> parameters, String alias, String name,
			String operator) {
		if (b.length() == 0) {
			b.append(" where ");
		} else {
			b.append(" and ");
		}
		b.append(alias);
		b.append(".");
		b.append(name);
		b.append(" ");
		b.append(operator);
	}

	static public void formatUnaryOr(StringBuffer b,
			Map<String, Object> parameters, String alias, String name,
			String operator) {
		if (b.length() == 0) {
			b.append(" where ");
		} else {
			b.append(" or ");
		}
		b.append(alias);
		b.append(".");
		b.append(name);
		b.append(" ");
		b.append(operator);
	}

	/**
	 * Add an OR parameter.
	 * 
	 * @param b
	 * @param parameters
	 * @param alias
	 * @param name
	 * @param operator
	 * @param value
	 */
	static public void formatOr(StringBuffer b, Map<String, Object> parameters,
			String alias, String name, String operator, Object value) {
		if (value != null) {
			if (b.length() == 0) {
				b.append(" where ");
			} else {
				b.append(" or ");
			}
			b.append(alias);
			b.append(".");
			b.append(name);
			b.append(" ");
			b.append(operator);
			b.append(" :");
			b.append(name);
			if ("like".equals(operator)) {
				parameters.put(name, "%" + value + "%");
			} else {
				parameters.put(name, value);
			}
		}
	}

	/**
	 * Add a between operator.
	 * 
	 * @param b
	 * @param parameters
	 * @param alias
	 * @param name
	 * @param operator
	 * @param value
	 */
	static public void formatBetween(StringBuffer b,
			Map<String, Object> parameters, String alias, String name,
			Object fromValue, Object toValue) {
		if (fromValue != null && toValue != null) {
			if (b.length() == 0) {
				b.append(" where ");
			} else {
				b.append(" and ");
			}
			b.append(alias);
			b.append(".");
			b.append(name);
			b.append(" between :");
			b.append("from" + name);
			b.append(" and :");
			b.append("to" + name);
			parameters.put("from" + name, fromValue);
			parameters.put("to" + name, toValue);
		}
	}

	/**
	 * Set all the parameters.
	 * 
	 * @param qry
	 * @param parameters
	 */
	static public void setParameters(Query qry, Map<String, Object> parameters) {
		for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			if (parameters.get(key) != null) {
				qry.setParameter(key, parameters.get(key));
			}
		}
	}
	
	public static String getCountEjbql(String ejbql, String from, String alias) {
		int fromLoc = ejbql.indexOf("from");
		int orderLoc = ejbql.indexOf("order by");
		if (orderLoc < 0)
			orderLoc = ejbql.length();
		return "select count(*) from " + from 
		       + " f where f in ( select distinct " + alias + " " + ejbql.substring(fromLoc, orderLoc) + ")"; 
	}
	
	public static String formatWildcard(String filter, boolean all) {
		if (filter == null || filter.trim().length() == 0) {
			return "%";
		} else {
			if (all) {
				return "%" + filter.trim().toLowerCase().replace("*", "%")
						+ "%";
			} else {
				return filter.trim().toLowerCase() + '%';
			}
		}
	}
}
